package com.apware.utils.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream; 
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map.Entry; 

import org.apache.log4j.Logger;

public class HttpUtil {

	public static Logger log = Logger.getLogger(HttpUtil.class);

	/**
	 * 提交post请求
	 * 
	 * @param url
	 * @param para
	 * @return
	 */
	public static String postByRaw(String url, HashMap<String, String> para) {

		String result = "";
		final String BOUNDARY = "----WebKitFormBoundaryE19zNvXGzXaLvS5C";

		try {
			URL u = new URL(url);
			HttpURLConnection con = (HttpURLConnection) u.openConnection();
			con.setRequestMethod("POST");
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(true);
			con.setReadTimeout(300000);

			// con.setRequestProperty("Content-Type", "application/xml");
			// con.setRequestProperty("Content-Type",
			// "application/x-www-form-urlencoded");
			con.setRequestProperty("Content-Type",
					"multipart/form-data; boundary=" + BOUNDARY);
			// con.setRequestProperty("Content-Disposition", "form-data");
			con.setRequestProperty("Charset", "UTF-8");
			con.connect();

			StringBuffer buffer = new StringBuffer();

			if (para != null) {
				String content = "";
				for (Entry<String, String> e : para.entrySet()) {
					buffer.append("--" + BOUNDARY + "\r\n");
					buffer.append("Content-Disposition: form-data; name=\"");
					buffer.append(e.getKey());
					buffer.append("\"\r\n\r\n");
					buffer.append(e.getValue());
					buffer.append("\r\n");
				}

				buffer.append("--" + BOUNDARY + "--\r\n");
				OutputStream outputStream = con.getOutputStream();
				// content =
				// "<span style=\"font-size:14px;\">"+buffer.toString()+"</span>";
				content = buffer.toString();
				outputStream.write(content.getBytes());
				// DataOutputStream output = new DataOutputStream(
				// con.getOutputStream());
				// OutputStreamWriter output = new OutputStreamWriter(
				// con.getOutputStream(), "UTF-8");

				// content = content.substring(1);
				// // output.writeBytes(content);
				// output.write(content);
				outputStream.flush();
				outputStream.close();
			}

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					con.getInputStream(), "UTF-8"));
			String line = "";
			String resp = "";
			while ((line = reader.readLine()) != null) {
				resp += line;
			}

			reader.close();

			return resp;

		} catch (Exception e) {
			log.error("http post失败", e);

		}

		return result;
	}

	/**
	 * 提交post请求
	 * 
	 * @param url
	 * @param para
	 * @return
	 */
	public static String post(String url, HashMap<String, String> para) {

		String result = "";

		try {
			URL u = new URL(url);
			HttpURLConnection con = (HttpURLConnection) u.openConnection();
			con.setRequestMethod("POST");
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(true);
			// con.setRequestProperty("Content-Type", "application/xml");
			con.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			// con.setRequestProperty("Content-Disposition", "form-data");
			con.setRequestProperty("Charset", "UTF-8");
			con.connect();

			if (para != null) {
				String content = "";
				// DataOutputStream output = new DataOutputStream(
				// con.getOutputStream());
				OutputStreamWriter output = new OutputStreamWriter(
						con.getOutputStream(), "UTF-8");

				for (Entry<String, String> e : para.entrySet()) {
					String value = "";
					if (e.getValue() != null) {
						value = e.getValue();
					}
					// content += "&" + e.getKey() + "=" +
					// URLEncoder.encode(value,"UTF-8");
					content += "&" + e.getKey() + "=" + value;

				}

				content = content.substring(1);				 
				// output.writeBytes(content);
				output.write(content);
				output.flush();
				output.close();
			}

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					con.getInputStream(), "UTF-8"));
			String line = "";
			while ((line = reader.readLine()) != null) {
				result += line;
			}

			reader.close();
		} catch (Exception e) {			
			log.error("http post失败！", e);

		}

		return result;
	}
}
